package com.wbq.datastructuredemo.activity;

import java.io.File;

import com.wbq.datastructuredemo.R;
import com.wbq.datastructuredemo.engine.SaveTextFileTask;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditActivity extends Activity {
	private static final String TAG = "EditActivity";
	private EditText et_edit_edittext = null;
	private Button bt_edit_run = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit);
		et_edit_edittext = (EditText) super.findViewById(R.id.et_edit_edittext);
		bt_edit_run = (Button) super.findViewById(R.id.bt_edit_run);
		int screenHeight = getWindowManager().getDefaultDisplay().getHeight();      // 屏幕高（像素，如：800p）
		Log.i(TAG, ""+screenHeight);
		this.et_edit_edittext.setHeight(screenHeight-300);
		this.bt_edit_run.setOnClickListener(new bt_edit_runOnClickListener());
		
	}
	private class bt_edit_runOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String content = et_edit_edittext.getText().toString();
			try {
				SaveTextFileTask.SaveTextFile(content, "//sdcard//run.c");
				Log.i(TAG, "保存成功");
				Intent intent = new Intent();
				intent.setAction("android.intent.action.VIEW");
				intent.addCategory("android.intent.category.DEFAULT");
				intent.setType("*/*");
				File file = new File(Environment.getExternalStorageDirectory(),"run.c");
				Uri data = Uri.fromFile(file);
				intent.setData(data);
				intent.setClassName("com.n0n3m4.droidc", "com.n0n3m4.droidc.CCompilerMain");
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				Toast.makeText(getApplicationContext(),"运行失败,请下载C4droid",Toast.LENGTH_SHORT).show();
			}
			
		}
		
	}
	
}
